
local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag (10 years)
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26							//number of time windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort total window size						
local country="US"								//select non-JP citing country


global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

local controls_fes="i.time science_cites originality inventors_citing i.wipo_citing_broad i.wipo_cited_broad"

//************************************************************************
// Table 1 (column 1) and Table A7 (all)
//************************************************************************

est clear 
foreach bc_threshold in . 100 50 20 {
	*take base set
	use "$DATA\twincites_applicant_did_prep.dta", clear
	
	drop if backcite_count_citing>`bc_threshold'

	* drop all but first citation from each geographical source for each family
	sort family_cited priority_citing_d
	duplicates drop family_cited nonJP, force

	*remove families that only had citations from one source

	duplicates tag family_cited, gen(tag)
	drop if tag==0
	drop tag

	*set up time parameter
	gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
	quietly: sum temp
	gen time=temp-r(min)

	eststo: xi: reg lag treated nonJP `controls_fes', vce(cluster inventor_country_citing_e)
	esttab using "$OUT\baseline_did.tex", replace ///
	b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
	keep(treated nonJP science_cites originality inventors_citing _cons) ///
	mlabel("All" "$\leq 100$" "$\leq 50$" "$\leq 20$") indicate("Time FEs = _Itime_*" "Citing field FEs = _Iwipo_citi*" "Cited field FEs = _Iwipo_cite*") alignment(D{.}{.}{-1})

	didregress (lag science_cites originality inventors_citing i.wipo_citing_broad i.wipo_cited_broad) (treated), group(inventor_country_citing_e) time(time)
	estat ptrends
}



//************************************************************************
// Table 5
//************************************************************************

local bc_threshold=100	
local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26							//number of times windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort window size						
local country="US"								//select non-JP citing country

est clear 
clear all

use "$DATA\twincites_applicant_did_prep_prenorm.dta", clear
drop if backcite_count_citing>`bc_threshold'
sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force
duplicates tag family_cited, gen(tag)
drop if tag==0
drop tag
keep family_cited
duplicates drop
save "$INT\twincites_did_cited_families.dta", replace

use "$DATA\twincites_applicant_did_prep_prenorm.dta", clear
joinby family_cited using "$INT\twincites_did_cited_families.dta"

drop if backcite_count_citing>`bc_threshold'

* Matrix of US/JP before/after AIPA BEFORE citation removal
tab nonJP after_change_cited

sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force 		
duplicates tag family_cited, gen(tag)			// tag when families have been cited by JP and US patent
drop if tag==0
duplicates drop family_cited, force

* Matrix of US/JP before/after AIPA AFTER citation removal
tab after_change_cited

  
 